package com.kancy.swing.lang;

import java.util.*;

/**
 * CollectionUtils
 *
 * @author kancy
 * @date 2020/2/16 6:05
 */
public class CollectionUtils {

    private static final Object[] EMPTY_OBJECT_ARRAY = {};

    /**
     * 是否为空
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection<?> collection) {
        return Objects.isNull(collection) || collection.isEmpty();
    }
    /**
     * 是否为空
     * @param map
     * @return
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return Objects.isNull(map) || map.isEmpty();
    }

    /**
     * 集合转数组
     * @param collection
     * @return
     */
    public static Object[] toArray(Collection<?> collection) {
        if (isEmpty(collection)){
            return EMPTY_OBJECT_ARRAY;
        }else {
            Object[] objects = new Object[collection.size()];
            int index = 0;
            for (Object item : collection) {
                objects[index++] = item;
            }
            return objects;
        }
    }

    /**
     * 集合转数组
     * @param objects
     * @return
     */
    public static List toList(Object ... objects) {
        return Arrays.asList(objects);
    }

}
